{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "H:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:19: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "H:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:24: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "    功能：Z评分模型，以及中国修正版\n",
    "    作者：hwang_zhicheng\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = [\"SimHei\"]\n",
    "plt.rcParams[\"axes.unicode_minus\"] = False\n",
    "\n",
    "# 读取上市公司数据\n",
    "# data_xls_1 = pd.read_excel(\"白酒上市公司交易额表.xls\")\n",
    "# data_xls_2 = pd.read_excel(\"白酒上市公司营收.xls\")\n",
    "data_xls_1 = pd.read_excel(\"ST+上市公司交易额表.xls\")\n",
    "data_xls_2 = pd.read_excel(\"ST+上市公司营收.xls\")\n",
    "\n",
    "# 读取上市公司数据\n",
    "data_1 = data_xls_1.ix[:,\n",
    "         ['收盘价_Clpr', '流通股_Trdshr', '已上市流通股_Lsttrdshr',\n",
    "          '年收益率_Yrret', '年无风险收益率_Yrrfret', '每股净资产(元/股)_NAPS', '日期_Date', '上市状态_Listedstate']].values\n",
    "\n",
    "# print(\"data_1\", data_1)\n",
    "data_2 = data_xls_2.ix[:,\n",
    "         ['流动负债合计(元)_Totcurlia', '非流动负债合计(元)_TotNcurlia', '负债合计(元)_Totlia',\n",
    "          '营业收入(元)_Incmope', '资本公积(元)_Capsur', '所有者权益合计(元)_TotSHE',\n",
    "          '利润总额(元)_Totalprf', '财务费用(元)_Finexp', '实收资本(或股本)(元)_Shrcap',\n",
    "          '流动资产合计(元)_Totcurass', '非流动资产合计(元)_TotNcurass', '资产总计(元)_Totass', '截止日期_EndDt', \n",
    "          '最新公司全称_Lcomnm'  ]].values\n",
    "\n",
    "# print(\"data_2\", data_2)\n",
    "# print(data_2)\n",
    "# print(data_3)\n",
    "\"\"\"\n",
    "资本公积(元)_Capsur\n",
    "所有者权益合计(元)_TotSHE\n",
    "利润总额(元)_Totalprf\n",
    "财务费用(元)_Finexp\n",
    "实收资本(或股本)(元)_Shrcap\n",
    "\n",
    "'流动负债合计(元)_Totcurlia', '非流动负债合计(元)_TotNCurLia', '负债合计(元)_TotLia', \n",
    "             '营业收入(元)_Incmope', '资本公积(元)_Capsur', '所有者权益合计(元)_TotSHE',\n",
    "             '利润总额(元)_Totalprf','财务费用(元)_Finexp', '实收资本(或股本)(元)_Shrcap', \n",
    "             '流动资产合计(元)_Totcurass', '非流动资产合计(元)_TotNcurass', '资产总计(元)_Totass', '截止日期_EndDt'\n",
    "\"\"\"\n",
    "\n",
    "# 公司数据\n",
    "# 收盘价_Clpr\n",
    "Clpr_list = data_1[:, 0]\n",
    "# print(\"Clpr_list\", Clpr_list)\n",
    "# 流通股_Trdshr\n",
    "Trdshr_list = data_1[:, 1]\n",
    "# print(\"Trdshr_list\", Trdshr_list)\n",
    "# 已上市流通股_Ltrdshr\n",
    "Ltrdshr_list = data_1[:, 2]\n",
    "# print(\"Ltrdshr_list\", Ltrdshr_list)\n",
    "# 每股净资产\n",
    "NAPS_list = data_1[:, 5]\n",
    "# print(\"NAPS_list\", NAPS_list)\n",
    "# 日期_Date\n",
    "date_list = data_1[:, 6]\n",
    "# print(\"date_list\", date_list)\n",
    "# print(date_list)\n",
    "# date_year = date[1].year\n",
    "# print(date_year)\n",
    "# 股权市场价值列表\n",
    "E_list = (Clpr_list * Ltrdshr_list) + (Trdshr_list - Ltrdshr_list) * NAPS_list\n",
    "# print(\"E_list\", E_list)\n",
    "# 流动负债合计(元)_Totcurlia\n",
    "SD_list = data_2[:, 0]\n",
    "# print(SD_li)\n",
    "# 非流动负债合计(元)_TotNCurLia\n",
    "LD_list = data_2[:, 1]\n",
    "# print(LD_li)\n",
    "# 负债合计(元)_TotLia\n",
    "D_list = data_2[:, 2]\n",
    "# print(\"D_list：\", D_list)\n",
    "# 营业收入(元)_Incmope\n",
    "Income_list = data_2[:, 3]\n",
    "# 资本公积(元)_Capsur\n",
    "Reserve_list = data_2[:, 4]\n",
    "# 所有者权益合计(元)_TotSHE\n",
    "Own_E_list = data_2[:, 5]\n",
    "# 利润总额(元)_Totalprf\n",
    "Totalprf_list = data_2[:, 6]\n",
    "# 财务费用(元)_Finexp\n",
    "Finexp_list = data_2[:, 7]\n",
    "# 实收资本(或股本)(元)_Shrcap\n",
    "Shrcap_list = data_2[:, 8]\n",
    "# 流动资产合计(元)_Totcurass\n",
    "Totcurass_list = data_2[:, 9]\n",
    "# 非流动资产合计(元)_TotNcurass\n",
    "TotNcurass_list = data_2[:, 10]\n",
    "# 资产总计(元)_Totass\n",
    "Totass_list = data_2[:, 11]\n",
    "# 截止日期_EndDt\n",
    "EndDt_list = data_2[:, 12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array(['Norm', 'ST', 'ST', 'Norm', 'Norm', 'ST', 'Norm', 'Norm', 'ST',\n",
       "        'Norm', 'Norm', 'ST', 'ST', 'ST', 'ST', 'Norm', 'ST', 'ST', 'Norm',\n",
       "        'Norm', 'ST', 'Norm', 'Norm', 'Norm', 'Norm', 'ST', 'ST', 'ST',\n",
       "        'Norm', 'ST', 'Norm', 'Norm', 'ST', 'ST', 'Norm', 'ST', 'Norm',\n",
       "        'Norm', 'Norm', 'ST', 'Norm', 'Norm', 'Norm', 'Norm', 'ST', 'Norm',\n",
       "        'Norm', 'ST', 'Norm', 'Norm', 'Norm', 'Norm', 'Norm', 'ST', 'Norm',\n",
       "        'ST', 'ST', 'Norm', 'Norm', 'ST', 'Norm', 'Norm', 'ST'],\n",
       "       dtype=object)]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上市状态_Listedstate\n",
    "State_list = pd.DataFrame(data_1[:, 7])\n",
    "State_list = State_list.replace(\"*ST\", \"ST\")\n",
    "State_list = list(np.array(State_list).reshape(1,-1))\n",
    "State_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最新公司全称_Lcomnm\n",
    "Lconnm_list = list(data_2[:, 13][::3])\n",
    "len(Lconnm_list)\n",
    "Lconnm_list.replace(\"*ST\", \"ST\", inplace=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 1\n",
      "i = 0\n",
      "Z_list_length = 2\n",
      "i = 1\n",
      "Z_list_length = 3\n",
      "i = 2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "H:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:19: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "H:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:24: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "H:\\Anaconda\\lib\\site-packages\\pandas\\core\\indexing.py:116: FutureWarning: \n",
      "Passing list-likes to .loc or [] with any missing label will raise\n",
      "KeyError in the future, you can use .reindex() as an alternative.\n",
      "\n",
      "See the documentation here:\n",
      "https://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate-loc-reindex-listlike\n",
      "  return self._getitem_tuple(key)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 4\n",
      "i = 3\n",
      "Z_list_length = 5\n",
      "i = 4\n",
      "Z_list_length = 6\n",
      "i = 5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 7\n",
      "i = 6\n",
      "Z_list_length = 8\n",
      "i = 7\n",
      "Z_list_length = 9\n",
      "i = 8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 10\n",
      "i = 9\n",
      "Z_list_length = 11\n",
      "i = 10\n",
      "Z_list_length = 12\n",
      "i = 11\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 13\n",
      "i = 12\n",
      "Z_list_length = 14\n",
      "i = 13\n",
      "Z_list_length = 15\n",
      "i = 14\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 16\n",
      "i = 15\n",
      "Z_list_length = 17\n",
      "i = 16\n",
      "Z_list_length = 18\n",
      "i = 17\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 19\n",
      "i = 18\n",
      "Z_list_length = 20\n",
      "i = 19\n",
      "Z_list_length = 21\n",
      "i = 20\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 22\n",
      "i = 21\n",
      "Z_list_length = 23\n",
      "i = 22\n",
      "Z_list_length = 24\n",
      "i = 23\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 25\n",
      "i = 24\n",
      "Z_list_length = 26\n",
      "i = 25\n",
      "Z_list_length = 27\n",
      "i = 26\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 28\n",
      "i = 27\n",
      "Z_list_length = 29\n",
      "i = 28\n",
      "Z_list_length = 30\n",
      "i = 29\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 31\n",
      "i = 30\n",
      "Z_list_length = 32\n",
      "i = 31\n",
      "Z_list_length = 33\n",
      "i = 32\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 34\n",
      "i = 33\n",
      "Z_list_length = 35\n",
      "i = 34\n",
      "Z_list_length = 36\n",
      "i = 35\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 37\n",
      "i = 36\n",
      "Z_list_length = 38\n",
      "i = 37\n",
      "Z_list_length = 39\n",
      "i = 38\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 40\n",
      "i = 39\n",
      "Z_list_length = 41\n",
      "i = 40\n",
      "Z_list_length = 42\n",
      "i = 41\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 43\n",
      "i = 42\n",
      "Z_list_length = 44\n",
      "i = 43\n",
      "Z_list_length = 45\n",
      "i = 44\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 46\n",
      "i = 45\n",
      "Z_list_length = 47\n",
      "i = 46\n",
      "Z_list_length = 48\n",
      "i = 47\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 49\n",
      "i = 48\n",
      "Z_list_length = 50\n",
      "i = 49\n",
      "Z_list_length = 51\n",
      "i = 50\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 52\n",
      "i = 51\n",
      "Z_list_length = 53\n",
      "i = 52\n",
      "Z_list_length = 54\n",
      "i = 53\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 55\n",
      "i = 54\n",
      "Z_list_length = 56\n",
      "i = 55\n",
      "Z_list_length = 57\n",
      "i = 56\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 58\n",
      "i = 57\n",
      "Z_list_length = 59\n",
      "i = 58\n",
      "Z_list_length = 60\n",
      "i = 59\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list_length = 61\n",
      "i = 60\n",
      "Z_list_length = 62\n",
      "i = 61\n",
      "Z_list_length = 63\n",
      "i = 62\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z_list = [0.45184353431101004, 0.30024205830802136, -0.011140000153901451, 0.39335726839510643, 0.3305608867452099, 0.19456118438766845, 0.13730128302654396, 0.15534574121037606, 0.25682314703781794, 0.23863024991846585, 0.2617620329824176, 0.1417691187483442, 0.1967373079330454, 0.1944993663077162, 0.024624244084021456, 0.2701359348182056, 0.2948098995125572, 0.259634760642394, 0.2844441572141598, 0.3798880336612077, 0.5052167189250307, 0.2787534049715625, 0.32929946545724315, 0.5041275189931177, 0.3808558646281282, 0.381781706678338, 0.5041759941879723, 0.20570207980838, 0.681664976406787, 0.2908005485100072, 0.182935957936701, 0.223367854989296, -0.036223789299719894, 0.31745123906289074, 0.1329667453068512, 0.09564360042154148, 0.32875399898785673, 0.3662810287606665, 0.3273315228454779, 0.14788417279853688, 0.3038281883673336, 0.10877507725046665, 0.11473079500734398, 0.11397403914926993, 0.12622900456668407, 0.6509073147428779, 0.757267369737785, 0.827797958194279, 0.13893325433568543, 0.13564544376278825, 0.1107881269771028, 0.3678606197265215, 0.4335945929118576, 0.605188055406541, 0.2948507141011911, 0.24997749473514408, 0.29813826511069313, 0.1601348948355754, 0.026526713855158403, 0.02704090244667507, 0.17477677714154838, 0.13059380465720752, 0.12463905003473283]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "Z计分模型的判别函数如下:\n",
    "      Z =0.012X1+0.014X2+0.033X3+0.006X4+0.999X5\n",
    " 2.Z计分模型应用分析的前期准备\n",
    "    Z计分模型主要用于预测企业财务失败或破产的可能性，也可用于判定企业经营的状况，是目前在财务分析中最常用的一种模型，故本文首先用z计分模型来进行判别分析。先根据z计分模型分别计算三家乳品企业的z值，再按z值对企业进行比较和分析。\n",
    "其中：\n",
    "    X1=营运资金/资产总额=(流动资产一流动负债)/资产总额\n",
    "    该比率反映企业资产的流动性和分布状况，比率越高说明资产的流动性越强，财务失败的可能越小：\n",
    "    X2=留存收益/资产总额=(股东权益一股本一资本公积)/资产总额\n",
    "    该比率反映企业的积累水平，比率越高说明企业的积累水平越高，财务失败的可能越小：\n",
    "    X3=息税前利润/资产总额=(利润总额+利息费用)/资产总额\n",
    "    该比率反映企业的获利水平，比率越高说明企业的获利能力越强，财务失败的可能越小：\n",
    "    X4=股东权益市价/负债总额=（非流通股总股数+流通股）*每股市价/负债总额\n",
    "    该比率反映企业所有者权益(或净产)与企业债务之间的关系，比率越高，说明企业所有者权益越高或净资产越高，企业财务失败的可能性就越小\n",
    "    X5=营业收入/资产总额：\n",
    "    该比率反映企业总资产的周转速度或营运能力，比率越高说明企业的资产利用率越高，效果也越好。\n",
    "    （X3中的利息费用无法直接从年报中获取，故以财务费用代替，对结果应无实质性影响;X4中的每股市价以股票当年股市收盘价计算。）\n",
    "# 股权市场价值列表\n",
    "E_list = (Clpr_list * Ltrdshr_list) + (Trdshr_list - Ltrdshr_list) * NAPS_list\n",
    "\n",
    "# 流动负债合计(元)_Totcurlia\n",
    "SD_list = data_2[:, 0]\n",
    "# print(SD_li)\n",
    "# 非流动负债合计(元)_TotNCurLia\n",
    "LD_list = data_2[:, 1]\n",
    "# print(LD_li)\n",
    "# 负债合计(元)_TotLia\n",
    "D_list = data_2[:, 2]\n",
    "# 营业收入(元)_Incmope\n",
    "Income_list = data_2[:, 3]\n",
    "# 资本公积(元)_Capsur\n",
    "Reserve_list = data_2[:, 4]\n",
    "# 所有者权益合计(元)_TotSHE\n",
    "Own_E_list = data_2[:, 5]\n",
    "# 利润总额(元)_Totalprf\n",
    "Totalprf_list = data_2[:, 6]\n",
    "# 财务费用(元)_Finexp\n",
    "Finexp_list = data_2[:, 7]\n",
    "# 实收资本(或股本)(元)_Shrcap\n",
    "Shrcap_list = data_2[:, 8]\n",
    "# 流动资产合计(元)_Totcurass\n",
    "Totcurass_list = data_2[:, 9]\n",
    "# 非流动资产合计(元)_TotNcurass\n",
    "TotNcurass_list = data_2[:, 10]\n",
    "# 资产总计(元)_Totass\n",
    "Totass_list = data_2[:, 11]\n",
    "# 截止日期_EndDt\n",
    "EndDt_list = data_2[:, 12]\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "def Z_count(X):\n",
    "    \"\"\"\n",
    "        功能：计算Z值并返回\n",
    "    \"\"\"\n",
    "    Z = 0.012 * X[0] + 0.014 * X[1] + 0.033 * X[2] + 0.006 * X[3] + 0.999 * X[4]\n",
    "    return Z\n",
    "\n",
    "\n",
    "def read_data():\n",
    "    \"\"\"\n",
    "        功能：读取数据\n",
    "    \"\"\"\n",
    "    pass\n",
    "\n",
    "\n",
    "def X_count(i):\n",
    "    \"\"\"\n",
    "        功能：计算x1~x5的值\n",
    "        X1=营运资金/资产总额=(流动资产一流动负债)/资产总额\n",
    "        该比率反映企业资产的流动性和分布状况，比率越高说明资产的流动性越强，财务失败的可能越小：\n",
    "        X2=留存收益/资产总额=(股东权益一股本一资本公积)/资产总额\n",
    "        该比率反映企业的积累水平，比率越高说明企业的积累水平越高，财务失败的可能越小：\n",
    "        X3=息税前利润/资产总额=(利润总额+利息费用)/资产总额\n",
    "        该比率反映企业的获利水平，比率越高说明企业的获利能力越强，财务失败的可能越小：\n",
    "        X4=股东权益市价/负债总额=（非流通股总股数+流通股）*每股市价/负债总额\n",
    "        该比率反映企业所有者权益(或净产)与企业债务之间的关系，比率越高，说明企业所有者权益越高或净资产越高，企业财务失败的可能性就越小\n",
    "        X5=营业收入/资产总额：\n",
    "        该比率反映企业总资产的周转速度或营运能力，比率越高说明企业的资产利用率越高，效果也越好。\n",
    "        （X3中的利息费用无法直接从年报中获取，故以财务费用代替，对结果应无实质性影响;X4中的每股市价以股票当年股市收盘价计算。）\n",
    "    \"\"\"\n",
    "    x1 = (Totcurass_list[i] - SD_list[i]) / Totass_list[i]\n",
    "    x2 = (Own_E_list[i] - Shrcap_list[i] - Reserve_list[i]) / Totass_list[i]\n",
    "    x3 = (Totalprf_list[i] + Finexp_list[i]) / Totass_list[i]\n",
    "    x4 = E_list[i] / D_list[i]\n",
    "    x5 = Income_list[i] / Totass_list[i]\n",
    "    # print(\"[x1, x2, x3, x4, x5]\", [x1, x2, x3, x4, x5])\n",
    "    return [x1, x2, x3, x4, x5]\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "Z<2.675，借款被划入违约组；\n",
    "反之，如果Z≥2.675，则借款人被划入非违约组。\n",
    "当1.81<Z<2.99阿尔特曼发现此时的判断失误比较大，称该重叠区域为未知区\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "def main():\n",
    "    \"\"\"\n",
    "        主函数\n",
    "    \"\"\"\n",
    "    Company_name = [\"江苏保千里视像科技集团股份有限公司\",\n",
    "                    \"大唐电信科技股份有限公司\",\n",
    "                    \"哈尔滨空调股份有限公司\",\n",
    "                    \"罗顿发展股份有限公司\",\n",
    "                    \"吉林成城集团股份有限公司\",\n",
    "                    \"亿阳信通股份有限公司\",\n",
    "                    \"安源煤业集团股份有限公司\",\n",
    "                    \"抚顺特殊钢股份有限公司\",\n",
    "                    \"柳州化工股份有限公司\",\n",
    "                    \"太原狮头水泥股份有限公司\",\n",
    "                    \"上海中毅达股份有限公司\",\n",
    "                    \"上海富控互动娱乐股份有限公司\",\n",
    "                    \"湖南天雁机械股份有限公司\",\n",
    "                    \"哈尔滨工大高新技术产业开发股份有限公司\",\n",
    "                    \"西藏旅游股份有限公司\",\n",
    "                    \"新疆友好(集团)股份有限公司\",\n",
    "                    \"山东天业恒基股份有限公司\",\n",
    "                    \"中石化石油工程技术服务股份有限公司\",\n",
    "                    \"中国嘉陵工业股份有限公司(集团)\",\n",
    "                    \"览海医疗产业投资股份有限公司\",\n",
    "                    \"甘肃蓝科石化高新装备股份有限公司\"]\n",
    "\n",
    "    # Company_name = [\"新疆伊力特实业股份有限公司\",\n",
    "    #                 \"安徽金种子酒业股份有限公司\",\n",
    "    #                 \"贵州茅台酒股份有限公司\",\n",
    "    #                 \"河北衡水老白干酒业股份有限公司\",\n",
    "    #                 \"舍得酒业股份有限公司\",\n",
    "    #                 \"四川水井坊股份有限公司\",\n",
    "    #                 \"山西杏花村汾酒厂股份有限公司\",\n",
    "    #                 \"安徽迎驾贡酒股份有限公司\",\n",
    "    #                 \"江苏今世缘酒业股份有限公司\",\n",
    "    #                 \"安徽口子酒业股份有限公司\",\n",
    "    #                 \"金徽酒股份有限公司\"]\n",
    "    Z_list = []\n",
    "    number = 0\n",
    "\n",
    "    for i in range(int(data_1.shape[0])):\n",
    "        X = X_count(i)\n",
    "        Z = Z_count(X)\n",
    "\n",
    "        Z_list.append(Z)\n",
    "        Z_list_length = len(Z_list)\n",
    "\n",
    "        print(\"Z_list_length = {}\".format(Z_list_length))\n",
    "        print(\"i = {}\".format(i))\n",
    "\n",
    "        # print(\"Z_list = {}\".format(Z_list[i - 2: i]))\n",
    "\n",
    "        predict_list = []\n",
    "        if Z_list_length % 3 == 0 and Z_list_length != 0:\n",
    "            # print(\"Z_list\", Z_list)\n",
    "            plt_tle = Company_name[number] + \"16年到18年Z值统计\"\n",
    "            height_list = Z_list[i - 2: i + 1]\n",
    "            # 绘制条形图\n",
    "            rects1 = plt.bar(x=range(1, 6, 2), height=height_list, width=1, alpha=0.8, color='red')\n",
    "            plt.ylim(0, 1)  # y轴取值范围\n",
    "            # 设置x轴坐标点显示\n",
    "            tick_labels = [\"2016\", \"2017\", \"2018\"]\n",
    "            tick_pos = np.arange(1, 7, 2)\n",
    "            plt.xticks(tick_pos, tick_labels)\n",
    "            plt.title(plt_tle, size=16)\n",
    "            plt.xlabel(\"年份\", size=10)\n",
    "            plt.ylabel(\"Z值\", size=10)\n",
    "            # plt.legend()     # 设置题注\n",
    "\n",
    "            for a, b in zip(tick_pos, height_list):\n",
    "                plt.text(a, b + 0.02, '%.4f' % b, ha='center', va='bottom', fontsize=10)\n",
    "\n",
    "            plt.show()\n",
    "            # Z列表初始化\n",
    "            # Z_list = []\n",
    "            number += 1\n",
    "\n",
    "    print(\"Z_list = {}\".format(Z_list))\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ab\n"
     ]
    }
   ],
   "source": [
    "a = \"a\" + \"b\"\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2817166389.98 4200000.0 2821366389.98 2855262562.67 690772391.99\n",
      "  4113042202.77 693193556.08 42616216.68 2437886049 5242973934.91\n",
      "  1691434657.84 6934408592.75 Timestamp('2016-09-30 00:00:00')]\n",
      " [4021982573.59 1278448704.91 5300431278.5 2961030965.37 736318883.89\n",
      "  5270574632.44 365905316.74 216338704.16 2437886049 5085500662.99\n",
      "  5485505247.95 10571005910.94 Timestamp('2017-09-30 00:00:00')]\n",
      " [4202045253.56 1199504967.23 5401550220.79 115562775.0 711996924.25\n",
      "  -3552999832.25 -374900511.34 229296357.29 2437886049 1244751286.64\n",
      "  603799101.9 1848550388.54 Timestamp('2018-09-30 00:00:00')]\n",
      " [6088357211.57 3559225877.73 9647583089.3 5152385783.8 3693897995.09\n",
      "  3711436917.8 -600093199.89 224931793.98 882108472 7759732626.25\n",
      "  5599287380.85 13359020007.1 Timestamp('2016-09-30 00:00:00')]\n",
      " [7029422143.12 2379641243.31 9409063386.43 3869910074.83 3959917848.73\n",
      "  2357331520.41 -495232170.57 189909595.06 882108472 6579757643.35\n",
      "  5186637263.49 11766394906.84 Timestamp('2017-09-30 00:00:00')]\n",
      " [4962953288.64 2680883429.49 7643836718.13 1570874026.13 3679604222.93\n",
      "  300341848.29 263014243.28 214768584.28 882108472 3577978115.49\n",
      "  4366200450.93 7944178566.42 Timestamp('2018-09-30 00:00:00')]\n",
      " [737480278.66 473742216.46 1211222495.12 218717589.57 76306605.52\n",
      "  780493094.55 -89936178.76 32533896.51 383340672 1287861021.19\n",
      "  703854568.48 1991715589.67 Timestamp('2016-09-30 00:00:00')]\n",
      " [978651817.6 17085432.38 995737249.98 229726536.76 77756487.92\n",
      "  642485832.0 -43719064.23 22599010.53 383340672 971753239.59\n",
      "  666469842.39 1638223081.98 Timestamp('2017-09-30 00:00:00')]\n",
      " [958387851.0 10364848.3 968752699.3 389409655.52 77781579.13\n",
      "  606493199.17 11714433.06 17229793.62 383340672 978755430.62\n",
      "  596490467.85 1575245898.47 Timestamp('2018-09-30 00:00:00')]\n",
      " [175147879.8 3879791.31 179027671.11 49278017.79 118386578.49\n",
      "  751866310.48 -59775324.68 -201247.41 439011169 250793202.48\n",
      "  680100779.11 930893981.59 Timestamp('2016-09-30 00:00:00')]\n",
      " [153762023.23 nan 153762023.23 75401860.86 118386578.49 713297427.82\n",
      "  -29641836.71 -62410.85 439011169 232428039.34 634631411.71 867059451.05\n",
      "  Timestamp('2017-09-30 00:00:00')]\n",
      " [145333007.1 nan 145333007.1 56336363.82 118386578.49 652404897.85\n",
      "  -33737446.02 8585.45 439011169 186883245.9 610854659.05 797737904.95\n",
      "  Timestamp('2018-09-30 00:00:00')]\n",
      " [933413360.83 34025666.22 967439027.05 173425784.02 30348878.64\n",
      "  41326644.89 -62976370.15 20893527.56 336441600 872226574.43\n",
      "  136539097.51 1008765671.94 Timestamp('2016-09-30 00:00:00')]\n",
      " [730160126.45 471012332.17 1201172458.62 185952189.85 219290712.51\n",
      "  -284730471.26 -368267248.27 31914258.7 336441600 848860032.52\n",
      "  67581954.84 916441987.36 Timestamp('2017-09-30 00:00:00')]\n",
      " [755443865.39 165427428.62 920871294.01 28081216.44 219290712.51\n",
      "  27497210.95 14643139.72 27594459.09 336441600 54826065.2 893542439.76\n",
      "  948368504.96 Timestamp('2018-09-30 00:00:00')]\n",
      " [876888396.06 nan 876888396.06 825903215.72 1475630289.71 3116119625.96\n",
      "  39818183.59 7627801.23 565922684 3121141077.48 871866944.54\n",
      "  3993008022.02 Timestamp('2016-09-30 00:00:00')]\n",
      " [653667666.08 nan 653667666.08 868035336.09 1410700908.71 3124173050.88\n",
      "  -40359462.74 -1180546.3 631052069 2864213161.59 913627555.37\n",
      "  3777840716.96 Timestamp('2017-09-30 00:00:00')]\n",
      " [591247431.56 1871488443.5 2462735875.06 717307428.4 1410500904.71\n",
      "  247199958.22 -458353166.34 -2671130.97 631052069 2076684657.08\n",
      "  633251176.2 2709935833.28 Timestamp('2018-09-30 00:00:00')]\n",
      " [4213675099.95 2156854129.2 6370529229.15 2581121159.88 1864598141.95\n",
      "  2776191940.88 -746710141.88 151115695.17 989959882 4208462114.27\n",
      "  4938259055.76 9146721170.03 Timestamp('2016-09-30 00:00:00')]\n",
      " [3916316838.98 2145997133.17 6062313972.15 2797525237.71 1863283020.38\n",
      "  1282666538.68 -89561164.75 153229665.8 989959882 2875241541.65\n",
      "  4469738969.18 7344980510.83 Timestamp('2017-09-30 00:00:00')]\n",
      " [3971996147.87 2157332618.17 6129328766.04 3512737919.18 1863283020.38\n",
      "  775779226.32 81087877.45 161674309.49 989959882 2791712283.02\n",
      "  4113395709.34 6905107992.36 Timestamp('2018-09-30 00:00:00')]\n",
      " [10002557599.71 598998913.91 10601556513.62 3475062985.76 162574353.9\n",
      "  2005804336.94 155847754.54 247819248.12 1300000000 7051368929.23\n",
      "  5555991921.33 12607360850.56 Timestamp('2016-09-30 00:00:00')]\n",
      " [9576378430.86 917310185.01 10493688615.87 4121419347.39 233974353.9\n",
      "  2123889529.31 80626482.41 298465862.36 1300000000 7014215554.3\n",
      "  5603362590.88 12617578145.18 Timestamp('2017-09-30 00:00:00')]\n",
      " [8469922848.34 991660005.25 9461582853.59 4352109426.82 462574353.9\n",
      "  -952881772.26 -126148805.76 289908500.46 1300000000 4113509124.68\n",
      "  4395191956.65 8508701081.33 Timestamp('2018-09-30 00:00:00')]\n",
      " [3501535228.91 350994287.54 3852529516.45 1628895430.27 509305943.3\n",
      "  358860872.67 -429380781.71 122569287.72 399347513 992621790.08\n",
      "  3218768599.04 4211390389.12 Timestamp('2016-09-30 00:00:00')]\n",
      " [2658653543.69 866539215.58 3525192759.27 1302053804.31 509305943.3\n",
      "  -200067948.31 -178997395.29 98116184.35 399347513 607382989.98\n",
      "  2717741820.98 3325124810.96 Timestamp('2017-09-30 00:00:00')]\n",
      " [2288188070.42 466664434.32 2754852504.74 1469312708.46 669282633.58\n",
      "  133214504.01 -85960621.4 23506407.81 399347513 972868298.51\n",
      "  1915198710.24 2888067008.75 Timestamp('2018-09-30 00:00:00')]\n",
      " [259022642.04 2414075.98 261436718.02 110720294.47 546357838.12\n",
      "  649127026.44 -54841481.44 374554.53 230000000 131392538.75 779171205.71\n",
      "  910563744.46 Timestamp('2016-09-30 00:00:00')]\n",
      " [40773748.63 6023.05 40779771.68 58890722.01 546357838.12 482310627.11\n",
      "  7031970.69 -4141918.38 230000000 405439923.81 117650474.98 523090398.79\n",
      "  Timestamp('2017-09-30 00:00:00')]\n",
      " [51914900.59 4058.05 51918958.64 59412942.24 546357838.12 438485672.98\n",
      "  14376082.23 -9935839.47 230000000 441700906.68 48703724.94 490404631.62\n",
      "  Timestamp('2018-09-30 00:00:00')]\n",
      " [258635709.39 4792877.0 263428586.39 119116826.19 382621301.81\n",
      "  1232483034.03 -8708174.35 1816714.06 1071274605 1336356754.09\n",
      "  159554866.33 1495911620.42 Timestamp('2016-09-30 00:00:00')]\n",
      " [835217978.24 51920152.0 887138130.24 440698352.96 382621301.81\n",
      "  1304226110.7 29135687.59 5611088.65 1071274605 2036671174.31\n",
      "  154693066.63 2191364240.94 Timestamp('2017-09-30 00:00:00')]\n",
      " [331844171.53 94390567.0 426234738.53 0.0 382621301.81 13315251.13\n",
      "  -21740954.33 12874941.12 1071274605 375242635.74 64307353.92\n",
      "  439549989.66 Timestamp('2018-09-30 00:00:00')]\n",
      " [3145750770.96 1611986106.21 4757736877.17 2239769052.46 1209429903.06\n",
      "  2691869229.05 73643648.16 163639309.33 575732081 4052438155.79\n",
      "  3397167950.43 7449606106.22 Timestamp('2016-09-30 00:00:00')]\n",
      " [1595716391.64 728711612.5 2324428004.14 621941573.82 1934598844.16\n",
      "  4215746351.12 213647061.35 116811505.0 575732081 3660049674.93\n",
      "  2880124680.33 6540174355.26 Timestamp('2017-09-30 00:00:00')]\n",
      " [4000723114.52 444112249.61 4444835364.13 596796190.19 987422679.57\n",
      "  1558618611.11 -425486918.94 235036468.57 575732081 1704338515.98\n",
      "  4299115459.26 6003453975.24 Timestamp('2018-09-30 00:00:00')]\n",
      " [653817624.25 21226321.19 675043945.44 406689053.95 374183145.15\n",
      "  659207292.07 8338487.36 3926077.25 971817440 1050344323.62 283906913.89\n",
      "  1334251237.51 Timestamp('2016-09-30 00:00:00')]\n",
      " [712013809.66 60437244.0 772451053.66 461719830.21 374183145.15\n",
      "  651956198.28 -8849111.62 4272259.82 971817440 1142269488.79\n",
      "  282137763.15 1424407251.94 Timestamp('2017-09-30 00:00:00')]\n",
      " [594435454.34 87719725.02 682155179.36 384004879.35 374183145.15\n",
      "  574844408.64 -2087648.87 2613187.84 971817440 991262174.99 265737413.01\n",
      "  1256999588.0 Timestamp('2018-09-30 00:00:00')]\n",
      " [1916765525.1 3131700000.0 5048465525.1 851159860.03 105932971.81\n",
      "  1172255936.4 -1469779.17 13469706.28 498781936 2193064300.16\n",
      "  4027657161.34 6220721461.5 Timestamp('2016-09-30 00:00:00')]\n",
      " [3096476818.36 1810800000.0 4907276818.36 2749268378.25 2784497157.81\n",
      "  4607123486.56 252715176.09 63005469.51 1034735218 4659464739.83\n",
      "  4854935565.09 9514400304.92 Timestamp('2017-09-30 00:00:00')]\n",
      " [4348051384.41 1486350000.0 5834401384.41 1044083788.37 2784497157.81\n",
      "  3998799450.27 -419772192.41 255101152.12 1034735218 4477002478.15\n",
      "  5356198356.53 9833200834.68 Timestamp('2018-09-30 00:00:00')]\n",
      " [427281175.14 342000000.0 769281175.14 114407701.06 439879411.71\n",
      "  608896972.56 -26562034.2 13523674.9 189137931 138747169.67\n",
      "  1239430978.03 1378178147.7 Timestamp('2016-09-30 00:00:00')]\n",
      " [488201585.41 372115596.06 860317181.47 127498329.07 439879411.71\n",
      "  521751981.08 -16248754.07 14398669.51 189137931 127371355.09\n",
      "  1254697807.46 1382069162.55 Timestamp('2017-09-30 00:00:00')]\n",
      " [430945392.62 22216000.0 453161392.62 147802527.62 979110519.49\n",
      "  1055033190.85 21728490.0 5123795.58 226965517 847975281.26 660219302.21\n",
      "  1508194583.47 Timestamp('2018-09-30 00:00:00')]\n",
      " [3052672230.37 1796344529.4 4849016759.77 4287240860.01 403604538.39\n",
      "  1779931669.7 -176942264.11 102446903.33 311491352 2276094376.98\n",
      "  4352854052.49 6628948429.47 Timestamp('2016-09-30 00:00:00')]\n",
      " [3623330721.01 989783147.4 4613113868.41 4232400516.62 403604538.39\n",
      "  953302587.31 -243591698.16 107999808.1 311491352 1510604767.63\n",
      "  4055811688.09 5566416455.72 Timestamp('2017-09-30 00:00:00')]\n",
      " [3117952444.25 1109725058.09 4227677502.34 4181882481.59 403604538.39\n",
      "  813564060.27 94721827.39 70788934.73 311491352 1328025520.76\n",
      "  3713216041.85 5041241562.61 Timestamp('2018-09-30 00:00:00')]\n",
      " [4766767804.31 1874782592.13 6641550396.44 1104293944.71 1113563228.51\n",
      "  2007844106.21 119096981.31 119936650.61 884634731 6195345871.46\n",
      "  2454048631.19 8649394502.65 Timestamp('2016-09-30 00:00:00')]\n",
      " [5627746431.9 2404801218.08 8032547649.98 1270711872.51 1164706628.88\n",
      "  2081179142.47 130047024.34 343009335.86 884634731 7722941912.7\n",
      "  2390784879.75 10113726792.45 Timestamp('2017-09-30 00:00:00')]\n",
      " [7308085249.93 483276529.81 7791361779.74 949851934.01 1182884873.41\n",
      "  724986549.68 -962522626.49 967380047.91 884634731 6742612220.58\n",
      "  1773736108.84 8516348329.42 Timestamp('2018-09-30 00:00:00')]\n",
      " [57612742000.0 859048000.0 58471790000.0 27781621000.0 8894216000.0\n",
      "  15870594000.0 -8674682000.0 335333000.0 14142661000 41142842000.0\n",
      "  33199542000.0 74342384000.0 Timestamp('2016-09-30 00:00:00')]\n",
      " [61543097000.0 1042804000.0 62585901000.0 30026964000.0 8906280000.0\n",
      "  5326223000.0 -3041290000.0 433261000.0 14142661000 37523295000.0\n",
      "  30388829000.0 67912124000.0 Timestamp('2017-09-30 00:00:00')]\n",
      " [52889697000.0 1622394000.0 54512091000.0 37181137000.0 11702532000.0\n",
      "  6208700000.0 668112000.0 223609000.0 18984340000 33402472000.0\n",
      "  27318319000.0 60720791000.0 Timestamp('2018-09-30 00:00:00')]\n",
      " [1744522375.21 82836911.4 1827359286.61 504208466.94 97570144.93\n",
      "  -96303861.04 -128165021.33 5421885.34 687282040 621335991.49\n",
      "  1109719434.08 1731055425.57 Timestamp('2016-09-30 00:00:00')]\n",
      " [1910099025.65 96096439.45 2006195465.1 446424274.38 99879623.47\n",
      "  -229788452.8 37470217.63 60425174.6 687282040 711131414.83\n",
      "  1065275597.47 1776407012.3 Timestamp('2017-09-30 00:00:00')]\n",
      " [1018561007.48 147742957.91 1166303965.39 309077087.2 118860813.83\n",
      "  -165670379.15 -160948134.82 8655596.94 687282040 297696889.91\n",
      "  702936696.33 1000633586.24 Timestamp('2018-09-30 00:00:00')]\n",
      " [1113151988.5 427305775.35 1540457763.85 642138769.72 1868570039.08\n",
      "  3545055987.26 -114767466.16 66621948.0 873286575 1336309498.14\n",
      "  3749204252.97 5085513751.11 Timestamp('2016-09-30 00:00:00')]\n",
      " [1682420255.64 274497480.16 1956917735.8 33372931.35 1822285999.99\n",
      "  2532453238.65 -135586635.11 56741467.83 869099075 3639761316.9\n",
      "  849609657.55 4489370974.45 Timestamp('2017-09-30 00:00:00')]\n",
      " [730785358.72 699531736.96 1430317095.68 40155017.05 1822285999.99\n",
      "  2014036185.85 44401413.98 48349529.52 869099075 2372295472.92\n",
      "  1072057808.61 3444353281.53 Timestamp('2018-09-30 00:00:00')]\n",
      " [803082312.9 91238399.2 894320712.1 391638025.99 996957126.51\n",
      "  1980005599.39 10219539.8 9323356.32 354528198 1561944633.94\n",
      "  1312381677.55 2874326311.49 Timestamp('2016-09-30 00:00:00')]\n",
      " [1058162928.72 245444799.64 1303607728.36 347304479.98 996957126.51\n",
      "  1843923754.59 14633753.36 20369012.59 354528198 1679423535.01\n",
      "  1468107947.94 3147531482.95 Timestamp('2017-09-30 00:00:00')]\n",
      " [1233889327.91 236426936.3 1470316264.21 359975468.63 996957126.51\n",
      "  1653610653.93 -104918885.12 17564244.59 354528198 1747615109.21\n",
      "  1376311808.93 3123926918.14 Timestamp('2018-09-30 00:00:00')]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "H:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "data_2 = data_xls_2.ix[:,\n",
    "         ['流动负债合计(元)_Totcurlia', '非流动负债合计(元)_TotNcurlia', '负债合计(元)_Totlia',\n",
    "          '营业收入(元)_Incmope', '资本公积(元)_Capsur', '所有者权益合计(元)_TotSHE',\n",
    "          '利润总额(元)_Totalprf', '财务费用(元)_Finexp', '实收资本(或股本)(元)_Shrcap',\n",
    "          '流动资产合计(元)_Totcurass', '非流动资产合计(元)_TotNcurass', '资产总计(元)_Totass', '截止日期_EndDt']].values\n",
    "\n",
    "print(data_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data_1 [[14.49 441000000 441000000 0.0164 0.029153 4.26\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [23.42 441000000 441000000 0.6335 0.043631 4.69\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [13.03 441000000 441000000 -0.433 0.03752 5.34\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [9.18 555775002 555775002 -0.0827 0.029153 4.02\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [7.69 555775002 555775002 -0.1623 0.043631 4.04\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [4.57 555775002 555775002 -0.4044 0.03752 4.03\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [334.15 1256197800 1256197800 0.5597 0.029153 54.64\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [697.49 1256197800 1256197800 1.1077 0.043631 67.15\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [590.01 1256197800 1256197800 -0.1383 0.03752 81.49\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [23.35 438060173 350000000 -0.0248 0.029153 3.6\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [30.97 438060173 350000000 0.3328 0.043631 3.8\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [12.36 666012392 613284242 -0.4348 0.03752 3.97\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [22.6 337300000 337300000 -0.0122 0.029153 6.82\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [46.66 337300000 337300000 1.0678 0.043631 7.21\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [22.82 337300000 337300000 -0.51 0.03752 7.24\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [19.11 488545698 488545698 0.5059 0.029153 2.85\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [46.8 488545698 488545698 1.4731 0.043631 3.05\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [31.67 488545698 488545698 -0.31 0.03752 3.56\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [25.02 865848266 865848266 0.315 0.029153 5.32\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [56.99 865848266 865848266 1.2998 0.043631 5.89\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [35.05 865848266 865848266 -0.3745 0.03752 6.92\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [21.55 800000000 166400000 -0.2598 0.029153 4.65\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [17.59 800000000 166400000 -0.1559 0.043631 4.86\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [14.11 800000000 800000000 -0.158 0.03752 5.07\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [13.12 1254500000 680750000 -0.0095 0.029153 3.59\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [15.51 1254500000 1254500000 0.1966 0.043631 4.1\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [14.49 1254500000 1254500000 -0.0496 0.03752 4.76\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [32.13 600000000 293524395 -0.2436 0.029153 6.95\n",
      "  Timestamp('2016-12-30 00:00:00')]\n",
      " [46.05 600000000 293524395 0.4472 0.043631 8.12\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [35.07 600000000 600000000 -0.2221 0.03752 9.63\n",
      "  Timestamp('2018-12-28 00:00:00')]\n",
      " [31.07 280000000 70000000 nan nan 5.64 Timestamp('2016-12-30 00:00:00')]\n",
      " [18.05 364000000 115838450 -0.237 0.043631 4.81\n",
      "  Timestamp('2017-12-29 00:00:00')]\n",
      " [11.68 364000000 115838450 -0.3396 0.03752 5.22\n",
      "  Timestamp('2018-12-28 00:00:00')]]\n",
      "data_2 [[478193714.35 19546841.87 497740556.22 1187061236.13 203605459.43\n",
      "  1888909869.24 328407079.63 -4218516.59 441000000 1816014562.11\n",
      "  570635863.35 2386650425.46 Timestamp('2016-09-30 00:00:00')]\n",
      " [517261512.22 17825200.02 535086712.24 1285974257.34 203605459.43\n",
      "  2081114395.21 351245826.96 -4046751.08 441000000 2067782129.55\n",
      "  548418977.9 2616201107.45 Timestamp('2017-09-30 00:00:00')]\n",
      " [553957601.45 13601263.5 567558864.95 1498110384.03 206112509.94\n",
      "  2423849098.57 431150804.42 -3419377.77 441000000 2295422661.53\n",
      "  695985301.99 2991407963.52 Timestamp('2018-09-30 00:00:00')]\n",
      " [790910739.82 144730490.48 935641230.3 1038263379.98 700802805.2\n",
      "  2237954755.74 14821873.21 -36316376.23 555775002 2109045849.56\n",
      "  1064550136.48 3173595986.04 Timestamp('2016-09-30 00:00:00')]\n",
      " [653195864.54 148159974.44 801355838.98 830846838.6 700802805.2\n",
      "  2249353097.58 14035016.38 -31422428.6 555775002 1831532143.05\n",
      "  1219176793.51 3050708936.56 Timestamp('2017-09-30 00:00:00')]\n",
      " [589218340.88 164607615.91 753825956.79 797939118.2 700802805.2\n",
      "  2247665195.11 10915651.2 -26740309.41 555775002 1764035378.02\n",
      "  1237455773.88 3001491151.9 Timestamp('2018-09-30 00:00:00')]\n",
      " [33846958824.06 15570000.0 33862528824.06 26631884032.43 1374964415.72\n",
      "  71283503461.07 18037984822.0 -1302024.0 1256197800 83502886329.66\n",
      "  21643145955.47 105146032285.13 Timestamp('2016-09-30 00:00:00')]\n",
      " [39383620859.07 15570000.0 39399190859.07 42450467500.01 1374964415.72\n",
      "  88380845274.13 28492837658.04 -39273204.75 1256197800 104803294790.2\n",
      "  22976741343.0 127780036133.2 Timestamp('2017-09-30 00:00:00')]\n",
      " [40975040327.23 15570000.0 40990610327.23 52241669986.01 1374964415.72\n",
      "  106157062048.93 35577318406.27 -4384064.29 1256197800 124830898967.33\n",
      "  22316773408.83 147147672376.16 Timestamp('2018-09-30 00:00:00')]\n",
      " [1501734187.77 15848000.0 1517582187.77 1655704246.64 685055044.26\n",
      "  1577176520.13 117979898.3 3444112.23 438060173 2358596292.45\n",
      "  736162415.45 3094758707.9 Timestamp('2016-09-30 00:00:00')]\n",
      " [1220765984.1 14880059.72 1235646043.82 1729139852.84 685055044.26\n",
      "  1663578562.13 166300545.99 -7324474.55 438060173 2136766149.88\n",
      "  762458456.07 2899224605.95 Timestamp('2017-09-30 00:00:00')]\n",
      " [2927065035.97 172911207.74 3099976243.71 2430768507.2 1237102825.26\n",
      "  2648646911.64 301947899.55 2329922.03 666012392 3175612457.95\n",
      "  2573010697.4 5748623155.35 Timestamp('2018-09-30 00:00:00')]\n",
      " [1435602347.45 3267959.91 1438870307.36 1099194756.48 801973437.98\n",
      "  2299928493.06 108604258.2 12608583.34 337300000 2597038714.19\n",
      "  1141760086.23 3738798800.42 Timestamp('2016-09-30 00:00:00')]\n",
      " [1837045293.53 64966411.64 1902011705.17 1243243733.16 835781745.48\n",
      "  2430247509.15 184130079.78 124372.44 337300000 3373852762.83\n",
      "  958406451.49 4332259214.32 Timestamp('2017-09-30 00:00:00')]\n",
      " [1804771117.39 54816605.11 1859587722.5 1589088694.32 841398062.37\n",
      "  2636203704.94 359985953.03 -3467716.36 337300000 3299113102.91\n",
      "  1196678324.53 4495791427.44 Timestamp('2018-09-30 00:00:00')]\n",
      " [550497934.34 13596521.44 564094455.78 794531374.91 399506088.31\n",
      "  1393830167.43 201840912.34 -3999079.13 488545698 1380183611.72\n",
      "  577741011.49 1957924623.21 Timestamp('2016-09-30 00:00:00')]\n",
      " [971427371.98 13671232.14 985098604.12 1471332967.94 399506088.31\n",
      "  1488155129.11 321109492.03 -7831915.13 488545698 1813540831.5\n",
      "  659712901.73 2473253733.23 Timestamp('2017-09-30 00:00:00')]\n",
      " [1191019887.23 32505219.44 1223525106.67 2138677308.7 399506088.31\n",
      "  1740142786.74 576505669.04 -10318335.66 488545698 2224720702.0\n",
      "  738947191.41 2963667893.41 Timestamp('2018-09-30 00:00:00')]\n",
      " [2314527135.19 1460719.03 2315987854.22 3400593440.79 240806934.27\n",
      "  4668642745.57 691459155.25 -9436571.16 865848266 4554707986.91\n",
      "  2429922612.88 6984630599.79 Timestamp('2016-09-30 00:00:00')]\n",
      " [3367673918.22 10527411.34 3378201329.56 4855928777.47 242206934.27\n",
      "  5197583197.17 1248711334.22 -9601051.83 865848266 5902073994.69\n",
      "  2673710532.04 8575784526.73 Timestamp('2017-09-30 00:00:00')]\n",
      " [4106276633.62 21001683.87 4127278317.49 6915445912.15 242144634.27\n",
      "  6137274922.77 1909780094.46 -10307166.77 865848266 7436690677.65\n",
      "  2827862562.61 10264553240.26 Timestamp('2018-09-30 00:00:00')]\n",
      " [1241026712.74 63657054.52 1304683767.26 2139140817.74 1263000533.82\n",
      "  3724568394.28 631708651.33 -11881351.14 800000000 3506230562.11\n",
      "  1523021599.43 5029252161.54 Timestamp('2016-09-30 00:00:00')]\n",
      " [1256699799.66 56071012.25 1312770811.91 2184376340.33 1263000533.82\n",
      "  3896455490.75 568348068.62 -11908797.42 800000000 3573101777.31\n",
      "  1636124525.35 5209226302.66 Timestamp('2017-09-30 00:00:00')]\n",
      " [1369003737.85 57886155.19 1426889893.04 2437188782.69 1263000533.82\n",
      "  4064659225.28 664315326.14 -7911026.02 800000000 3693932938.11\n",
      "  1797616180.21 5491549118.32 Timestamp('2018-09-30 00:00:00')]\n",
      " [1193274983.47 21250000.0 1214524983.47 2060363983.21 742713960.84\n",
      "  4515620773.94 866706316.45 -15839544.47 1254500000 4007139264.68\n",
      "  1723006492.73 5730145757.41 Timestamp('2016-09-30 00:00:00')]\n",
      " [1393163155.36 18375000.0 1411538155.36 2392920135.46 742713960.84\n",
      "  5153134654.42 1037019510.09 -9231789.12 1254500000 4457699063.98\n",
      "  2106973745.8 6564672809.78 Timestamp('2017-09-30 00:00:00')]\n",
      " [1733891129.97 17085808.52 1750976938.49 3158786047.46 717091873.41\n",
      "  5989212340.61 1370545737.03 -23922484.44 1254500000 5400940655.73\n",
      "  2339248623.37 7740189279.1 Timestamp('2018-09-30 00:00:00')]\n",
      " [1142646425.36 45000000.0 1187646425.36 2334965401.53 976882369.04\n",
      "  4170002800.09 950595099.14 -4788852.08 600000000 3535810577.61\n",
      "  1821838647.84 5357649225.45 Timestamp('2016-09-30 00:00:00')]\n",
      " [1687760459.6 45000000.0 1732760459.6 2715231243.88 976882369.04\n",
      "  4872076516.08 1186063746.04 -7862027.87 600000000 4549906485.32\n",
      "  2054930490.36 6604836975.68 Timestamp('2017-09-30 00:00:00')]\n",
      " [1746696705.31 65000000.0 1811696705.31 3207494339.15 976882369.04\n",
      "  5776107121.68 1545055254.34 -6583483.05 600000000 5195660150.77\n",
      "  2392143676.22 7587803826.99 Timestamp('2018-09-30 00:00:00')]\n",
      " [355125160.72 7094500.0 362219660.72 926365548.41 737978266.22\n",
      "  1580145182.6 214630956.11 10413174.92 280000000 662776533.01\n",
      "  1279588310.31 1942364843.32 Timestamp('2016-09-30 00:00:00')]\n",
      " [372592499.34 5415000.0 378007499.34 941504642.87 653978266.22\n",
      "  1751126011.17 231242497.2 -1428097.49 364000000 726904498.28\n",
      "  1402229012.23 2129133510.51 Timestamp('2017-09-30 00:00:00')]\n",
      " [540670686.31 306263000.0 846933686.31 965125173.03 653978266.22\n",
      "  1900470822.46 214747758.99 131156.6 364000000 1115086729.79\n",
      "  1632317778.98 2747404508.77 Timestamp('2018-09-30 00:00:00')]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:6: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  \n",
      "F:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:11: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  # This is added back by InteractiveShellApp.init_path()\n"
     ]
    }
   ],
   "source": [
    "# 读取上市公司数据\n",
    "data_xls_1 = pd.read_excel(\"白酒上市公司交易额表.xls\")\n",
    "data_xls_2 = pd.read_excel(\"白酒上市公司营收.xls\")\n",
    "\n",
    "# 读取上市公司数据\n",
    "data_1 = data_xls_1.ix[:,\n",
    "         ['收盘价_Clpr', '流通股_Trdshr', '已上市流通股_Lsttrdshr',\n",
    "          '年收益率_Yrret', '年无风险收益率_Yrrfret', '每股净资产(元/股)_NAPS', '日期_Date']].values\n",
    "\n",
    "print(\"data_1\", data_1)\n",
    "data_2 = data_xls_2.ix[:,\n",
    "         ['流动负债合计(元)_Totcurlia', '非流动负债合计(元)_TotNcurlia', '负债合计(元)_Totlia',\n",
    "          '营业收入(元)_Incmope', '资本公积(元)_Capsur', '所有者权益合计(元)_TotSHE',\n",
    "          '利润总额(元)_Totalprf', '财务费用(元)_Finexp', '实收资本(或股本)(元)_Shrcap',\n",
    "          '流动资产合计(元)_Totcurass', '非流动资产合计(元)_TotNcurass', '资产总计(元)_Totass', '截止日期_EndDt']].values\n",
    "\n",
    "print(\"data_2\", data_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    功能：Z评分模型\n",
    "    作者：hwang_zhicheng\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = [\"SimHei\"]\n",
    "plt.rcParams[\"axes.unicode_minus\"] = False\n",
    "\n",
    "# 读取上市公司数据\n",
    "# data_xls_1 = pd.read_excel(\"白酒上市公司交易额表.xls\")\n",
    "# data_xls_2 = pd.read_excel(\"白酒上市公司营收.xls\")\n",
    "data_xls_1 = pd.read_excel(\"ST+上市公司交易额表.xls\")\n",
    "data_xls_2 = pd.read_excel(\"ST+上市公司营收1.xls\")\n",
    "data_xls_3 = pd.read_excel(\"ST+财务比率.xls.xls\")\n",
    "\n",
    "# 读取上市公司数据\n",
    "data_1 = data_xls_1.ix[:,\n",
    "         [\"上市状态_Listedstate\"]].values\n",
    "\n",
    "# print(\"data_1\", data_1)\n",
    "data_2 = data_xls_2.ix[:,\n",
    "         ['流动负债合计(元)_Totcurlia', '负债合计(元)_Totlia','盈余公积(元)_Surres'\n",
    "          '流动资产合计(元)_Totcurass',  '资产总计(元)_Totass',\"最新公司全称_Lcomnm\"]].values\n",
    "\n",
    "data_3 = data_xls_3.ix[:,\n",
    "         ['息税前利润_EBIT', '股票市值/总负债(Ⅰ)_MarkValtotdb1']].values\n",
    "\n",
    "# print(\"data_2\", data_2)\n",
    "# print(data_2)\n",
    "# print(data_3)\n",
    "\"\"\"\n",
    "资本公积(元)_Capsur\n",
    "所有者权益合计(元)_TotSHE\n",
    "利润总额(元)_Totalprf\n",
    "财务费用(元)_Finexp\n",
    "实收资本(或股本)(元)_Shrcap\n",
    "\"\"\"\n",
    "\n",
    "# 公司数据\n",
    "\n",
    "# 上市状态_Listedstate\n",
    "State_list = data_1[:, 0]\n",
    "# State_list\n",
    "\n",
    "# 流动负债合计(元)_Totcurlia\n",
    "SD_list = data_2[:, 0]\n",
    "# print(SD_li)\n",
    "# 负债合计(元)_TotLia\n",
    "D_list = data_2[:, 1]\n",
    "# 盈余公积(元)_Surres\n",
    "Surres_list = data_2[:, 2]\n",
    "# print(\"D_list：\", D_list)\n",
    "# 流动资产合计(元)_Totcurass\n",
    "Totcurass_list = data_2[:, 3]\n",
    "# 资产总计(元)_Totass\n",
    "Totass_list = data_2[:, 4]\n",
    "# 最新公司全称_Lcomnm\n",
    "Lconnm_list = list(data_2[:, 5][::3])\n",
    "# Lconnm_list.replace(\"*ST\", \"ST\", inplace=1)\n",
    "# len(Lconnm_list)\n",
    "\n",
    "# 息税前利润_EBIT\n",
    "EBIT_list = data_3[:, 0]\n",
    "# 股票市值/总负债(Ⅰ)_MarkValtotdb1\n",
    "MarkValtotdb1_list = data_3[:, 1]\n",
    "\n",
    "\"\"\"\n",
    "Z计分模型的判别函数如下:\n",
    "      Z =0.012X1+0.014X2+0.033X3+0.006X4+0.999X5\n",
    " 2.Z计分模型应用分析的前期准备\n",
    "    Z计分模型主要用于预测企业财务失败或破产的可能性，也可用于判定企业经营的状况，是目前在财务分析中最常用的一种模型，故本文首先用z计分模型来进行判别分析。先根据z计分模型分别计算三家乳品企业的z值，再按z值对企业进行比较和分析。\n",
    "其中：\n",
    "    X1=营运资金/资产总额=(流动资产一流动负债)/资产总额\n",
    "    该比率反映企业资产的流动性和分布状况，比率越高说明资产的流动性越强，财务失败的可能越小：\n",
    "    X2=留存收益/资产总额=(股东权益一股本一资本公积)/资产总额\n",
    "    该比率反映企业的积累水平，比率越高说明企业的积累水平越高，财务失败的可能越小：\n",
    "    X3=息税前利润/资产总额=(利润总额+利息费用)/资产总额\n",
    "    该比率反映企业的获利水平，比率越高说明企业的获利能力越强，财务失败的可能越小：\n",
    "    X4=股东权益市价/负债总额=（非流通股总股数+流通股）*每股市价/负债总额\n",
    "    该比率反映企业所有者权益(或净产)与企业债务之间的关系，比率越高，说明企业所有者权益越高或净资产越高，企业财务失败的可能性就越小\n",
    "    X5=营业收入/资产总额：\n",
    "    该比率反映企业总资产的周转速度或营运能力，比率越高说明企业的资产利用率越高，效果也越好。\n",
    "    （X3中的利息费用无法直接从年报中获取，故以财务费用代替，对结果应无实质性影响;X4中的每股市价以股票当年股市收盘价计算。）\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "def Z_count(X):\n",
    "    \"\"\"\n",
    "        功能：计算Z值并返回\n",
    "    \"\"\"\n",
    "    # Z = 0.012 * X[0] + 0.014 * X[1] + 0.033 * X[2] + 0.006 * X[3] + 0.999 * X[4]\n",
    "    # Z = 0.065 * X[0] + 0.326 * X[1] + 0.01 * X[2] + 0.067 * X[3]\n",
    "    return Z\n",
    "\n",
    "\n",
    "def read_data():\n",
    "    \"\"\"\n",
    "        功能：读取数据\n",
    "    \"\"\"\n",
    "    pass\n",
    "\n",
    "\n",
    "def X_count(i):\n",
    "    \"\"\"\n",
    "        功能：计算x1~x5的值\n",
    "        Z=0.065×Xl+0.326×X2+0.01×X3+0.067×X4\n",
    "        其中：X1= (营运资产 / 总资产)×100\n",
    "        X2= 留存收益 / 总资产 = (盈余公积 + 未分配利润)/总资产×100\n",
    "        X3=税息前利润 / 总资产×100\n",
    "        X4= 资本市值 / 总负债×100\n",
    "    \"\"\"\n",
    "    x1 = (Totcurass_list[i] - SD_list[i]) / Totass_list[i]\n",
    "    x2 = (EBIT_list[i] + Surres_list[i]) / Totass_list[i]\n",
    "    x3 = (Totalprf_list[i] + Finexp_list[i]) / Totass_list[i]\n",
    "    x4 = E_list[i] / D_list[i]\n",
    "    print(\"[x1, x2, x3, x4\", [x1, x2, x3, x4])\n",
    "    return [x1, x2, x3, x4]\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "Z<2.675，借款被划入违约组；\n",
    "反之，如果Z≥2.675，则借款人被划入非违约组。\n",
    "当1.81<Z<2.99阿尔特曼发现此时的判断失误比较大，称该重叠区域为未知区\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "def main():\n",
    "    \"\"\"\n",
    "        主函数\n",
    "    \"\"\"\n",
    "    Company_name = Lconnm_list\n",
    "\n",
    "    Z_list = []\n",
    "    number = 0\n",
    "\n",
    "    for i in range(int(data_1.shape[0])):\n",
    "        X = X_count(i)\n",
    "        Z = Z_count(X)\n",
    "\n",
    "        Z_list.append(Z)\n",
    "        Z_list_length = len(Z_list)\n",
    "\n",
    "        # print(\"Z_list_length = {}\".format(Z_list_length))\n",
    "        # print(\"i = {}\".format(i))\n",
    "\n",
    "        # print(\"Z_list = {}\".format(Z_list[i - 2: i]))\n",
    "\n",
    "        if Z_list_length % 3 == 0 and Z_list_length != 0:\n",
    "            # print(\"Z_list\", Z_list)\n",
    "            plt_tle = Company_name[number] + \"16年到18年Z值统计\"\n",
    "            height_list = Z_list[i - 2: i + 1]\n",
    "            # 绘制条形图\n",
    "            rects1 = plt.bar(x=range(1, 6, 2), height=height_list, width=1, alpha=0.8, color='red')\n",
    "            plt.ylim(0, 1)  # y轴取值范围\n",
    "            # 设置x轴坐标点显示\n",
    "            tick_labels = [\"2016\", \"2017\", \"2018\"]\n",
    "            tick_pos = np.arange(1, 7, 2)\n",
    "            plt.xticks(tick_pos, tick_labels)\n",
    "            plt.title(plt_tle, size=16)\n",
    "            plt.xlabel(\"年份\", size=10)\n",
    "            plt.ylabel(\"Z值\", size=10)\n",
    "            # plt.legend()     # 设置题注\n",
    "\n",
    "            for a, b in zip(tick_pos, height_list):\n",
    "                plt.text(a, b + 0.02, '%.4f' % b, ha='center', va='bottom', fontsize=10)\n",
    "\n",
    "            plt.show()\n",
    "            # Z列表初始化\n",
    "            # Z_list = []\n",
    "            number += 1\n",
    "\n",
    "    print(\"Z_list = {}\".format(Z_list))\n",
    "    predict_list = []\n",
    "    error_num = 0\n",
    "    correct_num = 0\n",
    "    for i in range(len(Z_list)):\n",
    "        if Z_list[i] >= 2.675:\n",
    "            predict_list.append(\"Norm\")\n",
    "        elif Z_list[i] < 2.675:\n",
    "            predict_list.append(\"ST\")\n",
    "\n",
    "    print(\"State_list = {}\".format(State_list))\n",
    "    for i in range(len(predict_list)):\n",
    "        if predict_list[i] == State_list[i]:\n",
    "            correct_num += 1\n",
    "        else:\n",
    "            error_num += 1\n",
    "\n",
    "    print(\"predict_list = {}\".format(predict_list))\n",
    "    plt_tle = \"预测结果\"\n",
    "    height_list = [correct_num, error_num]\n",
    "    # 绘制条形图\n",
    "    rects1 = plt.bar(x=range(2), height=height_list, width=1, alpha=0.8, color='red')\n",
    "    # plt.ylim(0, 1)  # y轴取值范围\n",
    "    # 设置x轴坐标点显示\n",
    "    tick_labels = [\"correct_num\", \"error_num\"]\n",
    "    tick_pos = np.arange(2)\n",
    "    plt.xticks(tick_pos, tick_labels)\n",
    "    plt.title(plt_tle, size=16)\n",
    "    plt.xlabel(\"结果\", size=10)\n",
    "    plt.ylabel(\"计数\", size=10)\n",
    "    # plt.legend()     # 设置题注\n",
    "\n",
    "    for a, b in zip(tick_pos, height_list):\n",
    "        plt.text(a, b + 0.02, '%.4f' % b, ha='center', va='bottom', fontsize=10)\n",
    "\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n",
    "\n",
    "\"\"\"\n",
    "Z<2.675，借款被划入违约组；\n",
    "反之，如果Z≥2.675，则借款人被划入非违约组。\n",
    "当1.81<Z<2.99阿尔特曼发现此时的判断失误比较大，称该重叠区域为未知区\n",
    "\"\"\"\n",
    "\n",
    "# Company_name = [\"新疆伊力特实业股份有限公司\",\n",
    "#                 \"安徽金种子酒业股份有限公司\",\n",
    "#                 \"贵州茅台酒股份有限公司\",\n",
    "#                 \"河北衡水老白干酒业股份有限公司\",\n",
    "#                 \"舍得酒业股份有限公司\",\n",
    "#                 \"四川水井坊股份有限公司\",\n",
    "#                 \"山西杏花村汾酒厂股份有限公司\",\n",
    "#                 \"安徽迎驾贡酒股份有限公司\",\n",
    "#                 \"江苏今世缘酒业股份有限公司\",\n",
    "#                 \"安徽口子酒业股份有限公司\",\n",
    "#                 \"金徽酒股份有限公司\"]\n",
    "\n",
    "# [\"江苏保千里视像科技集团股份有限公司\",\n",
    "#             \"大唐电信科技股份有限公司\",\n",
    "#             \"哈尔滨空调股份有限公司\",\n",
    "#             \"罗顿发展股份有限公司\",\n",
    "#             \"吉林成城集团股份有限公司\",\n",
    "#             \"亿阳信通股份有限公司\",\n",
    "#             \"安源煤业集团股份有限公司\",\n",
    "#             \"抚顺特殊钢股份有限公司\",\n",
    "#             \"柳州化工股份有限公司\",\n",
    "#             \"太原狮头水泥股份有限公司\",\n",
    "#             \"上海中毅达股份有限公司\",\n",
    "#             \"上海富控互动娱乐股份有限公司\",\n",
    "#             \"湖南天雁机械股份有限公司\",\n",
    "#             \"哈尔滨工大高新技术产业开发股份有限公司\",\n",
    "#             \"西藏旅游股份有限公司\",\n",
    "#             \"新疆友好(集团)股份有限公司\",\n",
    "#             \"山东天业恒基股份有限公司\",\n",
    "#             \"中石化石油工程技术服务股份有限公司\",\n",
    "#             \"中国嘉陵工业股份有限公司(集团)\",\n",
    "#             \"览海医疗产业投资股份有限公司\",\n",
    "#             \"甘肃蓝科石化高新装备股份有限公司\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    功能：Z评分模型\n",
    "    作者：hwang_zhicheng\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = [\"SimHei\"]\n",
    "plt.rcParams[\"axes.unicode_minus\"] = False\n",
    "\n",
    "# 读取上市公司数据\n",
    "# data_xls_1 = pd.read_excel(\"白酒上市公司交易额表.xls\")\n",
    "# data_xls_2 = pd.read_excel(\"白酒上市公司营收.xls\")\n",
    "data_xls_1 = pd.read_excel(\"ST+上市公司交易额表.xls\")\n",
    "data_xls_2 = pd.read_excel(\"ST+上市公司营收.xls\")\n",
    "\n",
    "# 读取上市公司数据\n",
    "data_1 = data_xls_1.ix[:,\n",
    "         ['收盘价_Clpr', '流通股_Trdshr', '已上市流通股_Lsttrdshr',\n",
    "          '年收益率_Yrret', '年无风险收益率_Yrrfret', '每股净资产(元/股)_NAPS', '日期_Date', \"上市状态_Listedstate\"]].values\n",
    "\n",
    "# print(\"data_1\", data_1)\n",
    "data_2 = data_xls_2.ix[:,\n",
    "         ['流动负债合计(元)_Totcurlia', '非流动负债合计(元)_TotNcurlia', '负债合计(元)_Totlia',\n",
    "          '营业收入(元)_Incmope', '资本公积(元)_Capsur', '所有者权益合计(元)_TotSHE',\n",
    "          '利润总额(元)_Totalprf', '财务费用(元)_Finexp', '实收资本(或股本)(元)_Shrcap',\n",
    "          '流动资产合计(元)_Totcurass', '非流动资产合计(元)_TotNcurass', '资产总计(元)_Totass', '截止日期_EndDt', \"最新公司全称_Lcomnm\"]].values\n",
    "\n",
    "# print(\"data_2\", data_2)\n",
    "# print(data_2)\n",
    "# print(data_3)\n",
    "\"\"\"\n",
    "资本公积(元)_Capsur\n",
    "所有者权益合计(元)_TotSHE\n",
    "利润总额(元)_Totalprf\n",
    "财务费用(元)_Finexp\n",
    "实收资本(或股本)(元)_Shrcap\n",
    "\"\"\"\n",
    "\n",
    "# 公司数据\n",
    "# 收盘价_Clpr\n",
    "Clpr_list = data_1[:, 0]\n",
    "# print(\"Clpr_list\", Clpr_list)\n",
    "# 流通股_Trdshr\n",
    "Trdshr_list = data_1[:, 1]\n",
    "# print(\"Trdshr_list\", Trdshr_list)\n",
    "# 已上市流通股_Ltrdshr\n",
    "Ltrdshr_list = data_1[:, 2]\n",
    "# print(\"Ltrdshr_list\", Ltrdshr_list)\n",
    "# 每股净资产\n",
    "NAPS_list = data_1[:, 5]\n",
    "# print(\"NAPS_list\", NAPS_list)\n",
    "# 日期_Date\n",
    "date_list = data_1[:, 6]\n",
    "# 上市状态_Listedstate\n",
    "State_list = data_1[:, 7]\n",
    "# State_list\n",
    "# print(\"date_list\", date_list)\n",
    "# print(date_list)\n",
    "# date_year = date[1].year\n",
    "# print(date_year)\n",
    "# 股权市场价值列表\n",
    "E_list = (Clpr_list * Ltrdshr_list) + (Trdshr_list - Ltrdshr_list) * NAPS_list\n",
    "# print(\"E_list\", E_list)\n",
    "# 流动负债合计(元)_Totcurlia\n",
    "SD_list = data_2[:, 0]\n",
    "# print(SD_li)\n",
    "# 非流动负债合计(元)_TotNCurLia\n",
    "LD_list = data_2[:, 1]\n",
    "# print(LD_li)\n",
    "# 负债合计(元)_TotLia\n",
    "D_list = data_2[:, 2]\n",
    "# print(\"D_list：\", D_list)\n",
    "# 营业收入(元)_Incmope\n",
    "Income_list = data_2[:, 3]\n",
    "# 资本公积(元)_Capsur\n",
    "Reserve_list = data_2[:, 4]\n",
    "# 所有者权益合计(元)_TotSHE\n",
    "Own_E_list = data_2[:, 5]\n",
    "# 利润总额(元)_Totalprf\n",
    "Totalprf_list = data_2[:, 6]\n",
    "# 财务费用(元)_Finexp\n",
    "Finexp_list = data_2[:, 7]\n",
    "# 实收资本(或股本)(元)_Shrcap\n",
    "Shrcap_list = data_2[:, 8]\n",
    "# 流动资产合计(元)_Totcurass\n",
    "Totcurass_list = data_2[:, 9]\n",
    "# 非流动资产合计(元)_TotNcurass\n",
    "TotNcurass_list = data_2[:, 10]\n",
    "# 资产总计(元)_Totass\n",
    "Totass_list = data_2[:, 11]\n",
    "# 截止日期_EndDt\n",
    "EndDt_list = data_2[:, 12]\n",
    "# 最新公司全称_Lcomnm\n",
    "Lconnm_list = list(data_2[:, 13][::3])\n",
    "# Lconnm_list.replace(\"*ST\", \"ST\", inplace=1)\n",
    "# len(Lconnm_list)\n",
    "\"\"\"\n",
    "Z计分模型的判别函数如下:\n",
    "      Z =0.012X1+0.014X2+0.033X3+0.006X4+0.999X5\n",
    " 2.Z计分模型应用分析的前期准备\n",
    "    Z计分模型主要用于预测企业财务失败或破产的可能性，也可用于判定企业经营的状况，是目前在财务分析中最常用的一种模型，故本文首先用z计分模型来进行判别分析。先根据z计分模型分别计算三家乳品企业的z值，再按z值对企业进行比较和分析。\n",
    "其中：\n",
    "    X1=营运资金/资产总额=(流动资产一流动负债)/资产总额\n",
    "    该比率反映企业资产的流动性和分布状况，比率越高说明资产的流动性越强，财务失败的可能越小：\n",
    "    X2=留存收益/资产总额=(股东权益一股本一资本公积)/资产总额\n",
    "    该比率反映企业的积累水平，比率越高说明企业的积累水平越高，财务失败的可能越小：\n",
    "    X3=息税前利润/资产总额=(利润总额+利息费用)/资产总额\n",
    "    该比率反映企业的获利水平，比率越高说明企业的获利能力越强，财务失败的可能越小：\n",
    "    X4=股东权益市价/负债总额=（非流通股总股数+流通股）*每股市价/负债总额\n",
    "    该比率反映企业所有者权益(或净产)与企业债务之间的关系，比率越高，说明企业所有者权益越高或净资产越高，企业财务失败的可能性就越小\n",
    "    X5=营业收入/资产总额：\n",
    "    该比率反映企业总资产的周转速度或营运能力，比率越高说明企业的资产利用率越高，效果也越好。\n",
    "    （X3中的利息费用无法直接从年报中获取，故以财务费用代替，对结果应无实质性影响;X4中的每股市价以股票当年股市收盘价计算。）\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "def Z_count(X):\n",
    "    \"\"\"\n",
    "        功能：计算Z值并返回\n",
    "    \"\"\"\n",
    "    Z = 0.012 * X[0] + 0.014 * X[1] + 0.033 * X[2] + 0.006 * X[3] + 0.999 * X[4]\n",
    "    # Z = 0.065 * X[0] + 0.326 * X[1] + 0.01 * X[2] + 0.067 * X[3]\n",
    "    return Z\n",
    "\n",
    "\n",
    "def read_data():\n",
    "    \"\"\"\n",
    "        功能：读取数据\n",
    "    \"\"\"\n",
    "    pass\n",
    "\n",
    "\n",
    "def X_count(i):\n",
    "    \"\"\"\n",
    "        功能：计算x1~x5的值\n",
    "    \"\"\"\n",
    "    x1 = (Totcurass_list[i] - SD_list[i]) / Totass_list[i]\n",
    "    x2 = (Own_E_list[i] - Shrcap_list[i] - Reserve_list[i]) / Totass_list[i]\n",
    "    x3 = (Totalprf_list[i] + Finexp_list[i]) / Totass_list[i]\n",
    "    x4 = E_list[i] / D_list[i]\n",
    "    x5 = Income_list[i] / Totass_list[i]\n",
    "    print(\"[x1, x2, x3, x4, x5]\", [x1, x2, x3, x4, x5])\n",
    "    return [x1, x2, x3, x4, x5]\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "Z<2.675，借款被划入违约组；\n",
    "反之，如果Z≥2.675，则借款人被划入非违约组。\n",
    "当1.81<Z<2.99阿尔特曼发现此时的判断失误比较大，称该重叠区域为未知区\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "def main():\n",
    "    \"\"\"\n",
    "        主函数\n",
    "    \"\"\"\n",
    "    Company_name = Lconnm_list\n",
    "\n",
    "    Z_list = []\n",
    "    number = 0\n",
    "\n",
    "    for i in range(int(data_1.shape[0])):\n",
    "        X = X_count(i)\n",
    "        Z = Z_count(X)\n",
    "\n",
    "        Z_list.append(Z)\n",
    "        Z_list_length = len(Z_list)\n",
    "\n",
    "        # print(\"Z_list_length = {}\".format(Z_list_length))\n",
    "        # print(\"i = {}\".format(i))\n",
    "\n",
    "        # print(\"Z_list = {}\".format(Z_list[i - 2: i]))\n",
    "\n",
    "        if Z_list_length % 3 == 0 and Z_list_length != 0:\n",
    "            # print(\"Z_list\", Z_list)\n",
    "            plt_tle = Company_name[number] + \"16年到18年Z值统计\"\n",
    "            height_list = Z_list[i - 2: i + 1]\n",
    "            # 绘制条形图\n",
    "            rects1 = plt.bar(x=range(1, 6, 2), height=height_list, width=1, alpha=0.8, color='red')\n",
    "            plt.ylim(0, 1)  # y轴取值范围\n",
    "            # 设置x轴坐标点显示\n",
    "            tick_labels = [\"2016\", \"2017\", \"2018\"]\n",
    "            tick_pos = np.arange(1, 7, 2)\n",
    "            plt.xticks(tick_pos, tick_labels)\n",
    "            plt.title(plt_tle, size=16)\n",
    "            plt.xlabel(\"年份\", size=10)\n",
    "            plt.ylabel(\"Z值\", size=10)\n",
    "            # plt.legend()     # 设置题注\n",
    "\n",
    "            for a, b in zip(tick_pos, height_list):\n",
    "                plt.text(a, b + 0.02, '%.4f' % b, ha='center', va='bottom', fontsize=10)\n",
    "\n",
    "            plt.show()\n",
    "            # Z列表初始化\n",
    "            # Z_list = []\n",
    "            number += 1\n",
    "\n",
    "    print(\"Z_list = {}\".format(Z_list))\n",
    "    predict_list = []\n",
    "    error_num = 0\n",
    "    correct_num = 0\n",
    "    for i in range(len(Z_list)):\n",
    "        if Z_list[i] >= 2.675:\n",
    "            predict_list.append(\"Norm\")\n",
    "        elif Z_list[i] < 2.675:\n",
    "            predict_list.append(\"ST\")\n",
    "\n",
    "    print(\"State_list = {}\".format(State_list))\n",
    "    for i in range(len(predict_list)):\n",
    "        if predict_list[i] == State_list[i]:\n",
    "            correct_num += 1\n",
    "        else:\n",
    "            error_num += 1\n",
    "\n",
    "    print(\"predict_list = {}\".format(predict_list))\n",
    "    plt_tle = \"预测结果\"\n",
    "    height_list = [correct_num, error_num]\n",
    "    # 绘制条形图\n",
    "    rects1 = plt.bar(x=range(2), height=height_list, width=1, alpha=0.8, color='red')\n",
    "    # plt.ylim(0, 1)  # y轴取值范围\n",
    "    # 设置x轴坐标点显示\n",
    "    tick_labels = [\"correct_num\", \"error_num\"]\n",
    "    tick_pos = np.arange(2)\n",
    "    plt.xticks(tick_pos, tick_labels)\n",
    "    plt.title(plt_tle, size=16)\n",
    "    plt.xlabel(\"结果\", size=10)\n",
    "    plt.ylabel(\"计数\", size=10)\n",
    "    # plt.legend()     # 设置题注\n",
    "\n",
    "    for a, b in zip(tick_pos, height_list):\n",
    "        plt.text(a, b + 0.02, '%.4f' % b, ha='center', va='bottom', fontsize=10)\n",
    "\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n",
    "\n",
    "\"\"\"\n",
    "Z<2.675，借款被划入违约组；\n",
    "反之，如果Z≥2.675，则借款人被划入非违约组。\n",
    "当1.81<Z<2.99阿尔特曼发现此时的判断失误比较大，称该重叠区域为未知区\n",
    "\"\"\"\n",
    "\n",
    "# Company_name = [\"新疆伊力特实业股份有限公司\",\n",
    "#                 \"安徽金种子酒业股份有限公司\",\n",
    "#                 \"贵州茅台酒股份有限公司\",\n",
    "#                 \"河北衡水老白干酒业股份有限公司\",\n",
    "#                 \"舍得酒业股份有限公司\",\n",
    "#                 \"四川水井坊股份有限公司\",\n",
    "#                 \"山西杏花村汾酒厂股份有限公司\",\n",
    "#                 \"安徽迎驾贡酒股份有限公司\",\n",
    "#                 \"江苏今世缘酒业股份有限公司\",\n",
    "#                 \"安徽口子酒业股份有限公司\",\n",
    "#                 \"金徽酒股份有限公司\"]\n",
    "\n",
    "# [\"江苏保千里视像科技集团股份有限公司\",\n",
    "#             \"大唐电信科技股份有限公司\",\n",
    "#             \"哈尔滨空调股份有限公司\",\n",
    "#             \"罗顿发展股份有限公司\",\n",
    "#             \"吉林成城集团股份有限公司\",\n",
    "#             \"亿阳信通股份有限公司\",\n",
    "#             \"安源煤业集团股份有限公司\",\n",
    "#             \"抚顺特殊钢股份有限公司\",\n",
    "#             \"柳州化工股份有限公司\",\n",
    "#             \"太原狮头水泥股份有限公司\",\n",
    "#             \"上海中毅达股份有限公司\",\n",
    "#             \"上海富控互动娱乐股份有限公司\",\n",
    "#             \"湖南天雁机械股份有限公司\",\n",
    "#             \"哈尔滨工大高新技术产业开发股份有限公司\",\n",
    "#             \"西藏旅游股份有限公司\",\n",
    "#             \"新疆友好(集团)股份有限公司\",\n",
    "#             \"山东天业恒基股份有限公司\",\n",
    "#             \"中石化石油工程技术服务股份有限公司\",\n",
    "#             \"中国嘉陵工业股份有限公司(集团)\",\n",
    "#             \"览海医疗产业投资股份有限公司\",\n",
    "#             \"甘肃蓝科石化高新装备股份有限公司\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 6166683056 / 17743462709"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3475467645259614"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 6, 6, 4]\n"
     ]
    }
   ],
   "source": [
    "#map()函数映射求两个列表相乘\n",
    "\n",
    "func = lambda x,y:x*y\n",
    "result = map(func,[1,2,3,4],[4,3,2,1])\n",
    "list_result = list(result)\n",
    "print(list_result)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
